package cn.wolfcode.luowowo.mgrsite.controller;

import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.mgrsite.util.UploadUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UploadController {

    @Value("${file.path}")
    private String basePath;

    //upload
    @RequestMapping("/uploadImg")
    @ResponseBody
    public Object uploadImg(MultipartFile pic) {
        String upload = UploadUtil.upload(pic, basePath);
        return upload;
    }

    //uploadImg_ck
    @ResponseBody
    @RequestMapping("/uploadImg_ck")
    public Object uploadImg_ck(String module, MultipartFile upload) {
        HashMap<String, Object> map = new HashMap<>();
        String imagePath = "";
        if (upload.getSize() > 0 && upload != null) {
            try {
                imagePath = UploadUtil.upload(upload, basePath);
                map.put("uploaded", 1);
                map.put("url", "/" + imagePath);

            } catch (Exception e) {
                e.printStackTrace();
                map.put("uploaded", 0);
                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put("message",e.getMessage());
                map.put("error",objectMap);
            }
        }
        return map;
    }

}
